import { Component } from '@angular/core';

@Component({
  selector: 'app-sale-payment-detail',
  templateUrl: './sale-payment-detail.component.html',
  styleUrls: ['./sale-payment-detail.component.scss']
})
export class SalePaymentDetailComponent {
  // DialogService.open({ data }) 会把 data 赋给组件实例的 data 属性，这里直接接收
  data: any;

  // 兼容 data = { payment, order? } 或 data = payment 两种形态
  get payment(): any {
    return this.data?.payment ?? this.data ?? null;
  }

  get order(): any {
    return this.data?.order ?? null;
  }

  get title(): string {
    const p = this.payment;
    return p?.paymentNum ? `收款单：${p.paymentNum}` : '收款单详情';
  }

  get statusLabel(): string {
    const p = this.payment;
    if (!p) return '';
    if (p.status) return p.status;
    if (typeof p.isConfirmed === 'boolean') return p.isConfirmed ? '已确认' : '待确认';
    return '—';
  }

  get statusType(): 'success' | 'warning' | 'danger' | 'info' {
    const p = this.payment;
    if (!p) return 'info';
    const s = (p.status || '').toString();
    if (p.isConfirmed === true || /通过|完成|成功|已确认/.test(s)) return 'success';
    if (/拒绝|作废|失败/.test(s)) return 'danger';
    return 'warning';
  }

  formatAmount(v: any): string {
    const n = Number(v);
    if (isNaN(n)) return '—';
    return n.toLocaleString('zh-CN', { maximumFractionDigits: 2 });
  }

  getField(obj: any, key: string, fallback: string = '—'): any {
    if (!obj) return fallback;
    const v = obj[key];
    return v === null || v === undefined || v === '' ? fallback : v;
  }
}
